'use strict';
/**
 * 黄总--model类
 *2017.3.16
 */
export default class extends think.Model {
	
	/*
	**新增商品
	**输入：data=>{},
	**返回类型：return=>{success:1,info:'成功or失败'}
	*/
	async add(data){
		let res = await this.model('or_goods').add(data);
		return res;
	}


	/*
	**编辑商品
	**输入：data=>{},
	**返回类型：return=>{success:1,info:'成功or失败'}
	*/
	async update(data){
		//如果id不存在报错
		if(!data.id){
			return {success:0,info:"传值错误！"};
		};
		let is_exist = await this.model('or_goods').where({departmentid:data.departmentid,name:data.name,id:['!=',data.id]}).find();
		if(!think.isEmpty(is_exist)){
			return {success:0,info:"此产品已经存在！"};
		}
		let res = await this.model('or_goods').where({id:data.id}).update(data);
		return res;
	}

	/*
	**查找单个商品
	**输入：id,
	**返回类型：return=>goods
	*/
	async find(id){
		let ginfo = await this.model('or_goods').where({id:id}).find();
		return ginfo;
	}

	/*
	**输出商品列表
	**输入：机构的id
	**返回类型：{}
	*/
	async list(departmentid){
		//取出商品数据
		let glist = await this.model('or_goods')
			.alias('og')
			.field('og.id,og.name,og.price,ob.name as bname')
			.join('think_or_brand ob on og.brandid=ob.id')
			.where({'og.departmentid':departmentid})
			.select();
		return glist;
	}

	/*
	**删除单个商品
	**输入：商品id
	**返回类型：{}
	*/
	async delete(id){
		let res = await this.model('or_goods').where({id:id}).delete();
		return res?{success:1,info:"删除成功！"}:{success:0,info:"删除失败！"};
	}
}